function noop () { }

export const continuousClick = (() => {
  let count = 0
  let timer = null
  return (callback = noop, option) => {
    clearTimeout(timer)
    count++
    const initOption = {
      maxCount: 8,
      interval: 300
    }
    const mergeOption = Object.assign(initOption, option)
    timer = setTimeout(() => {
      count = 0
    }, mergeOption.interval)
    if (count >= mergeOption.maxCount) {
      callback()
    }
  }
})()

export const getNowFormatDate = () => {
  return format(Date.now(), 'YYYY-MM-DD HH:mm:ss')
}

export function convertName (name) {
  return name ? `$$logger_${name}` : '$$logger_base'
}

export function copyConfig (source, target = {}) {
  source = Object.assign(source, target)
  source.name = convertName(target.name)
}

// 时差处理，若当前环境不是北京时间，则调整时间到其对应时区时间显示
const bjOffset = -480
// 获取当前环境与0时区的时差 单位是分
const timeOffset = new Date().getTimezoneOffset() - bjOffset
function format (initDate, rule) {
  const date = new Date(initDate)
  rule = rule || 'YYYY-MM-DD HH:mm'
  date.setMinutes(date.getMinutes() + timeOffset)
  let prefix
  let index
  let temp = {
    Y: date.getFullYear(),
    M: date.getMonth() + 1,
    D: date.getDate(),
    d: date.getDay(),
    H: date.getHours(),
    m: date.getMinutes(),
    s: date.getSeconds(),
  }
  temp.h = temp.H % 12 === 0 ? 12 : temp.H % 12
  return rule.replace(/(Y+|M+|D+|d+|H+|m+|s+)/g, function (str) {
    prefix = str.length > 1 ? '0' : ''
    index = str.length > 2 ? str.length : 2
    return (prefix + temp[str.slice(-1)]).slice(-index)
  })
}